package org.flow.basic.doubts.tjbase.io;

import java.io.*;

/**
 * @author xl-9527
 * @since 2024/8/30
 */
public class BufferJavaIo implements JavaIo {

    @Override
    public void readFile(final String filePath, final int size) {
        final byte[] bytes = new byte[size == 0 ? 1024 : size];
        try (
                InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath + "io-buffer" + System.currentTimeMillis()))
        ) {
            while (inputStream.read(bytes) != -1) {
                // 执行保存逻辑
                outputStream.write(bytes);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void writeFile(final byte[] bytes, final FileOutputStream fileOutputStream) throws IOException {
        fileOutputStream.write(bytes);
    }
}
